<?php

class LoginController extends CController
{	
	var $connection = NULL;
	var $command = NULL;
	var $data;
	var $host_info = NULL;
		
	function init()
	{	
		$this->host_info = Yii::app()->getRequest()->getHostInfo();
		
		$this->connection = Yii::app()->db;
		$this->data = array();
		$website = website::model()->findByPk(1);
		$company = company::model()->findByPk(1);
		
		$this->data["website"] = $website;
		$this->data["company"] = $company;
		
		$controller_id = str_replace('controller','',strtolower(__CLASS__));
		$this->data['controller_id'] = $controller_id;
	}
	
	function actionIndex()
	{	
		// Load view without apply layout
		$this->renderPartial('login_screen', $this->data);
	}	
	
	function actionSign_in()
	{		
		$username = Yii::app()->getRequest()->getPost("username");
		$password = Yii::app()->getRequest()->getPost("password");
		
		//$username = addslashes ( $username );
		//$password = addslashes ( $password );
		
		// $username = mysql_real_escape_string($username);
		// $password = mysql_real_escape_string($password);
		
		$count = user::model()->count(array(
			"condition"=>"username = '$username' AND password = '" . md5($password) ."'" 
		));
		
		
		if ($count == 1) // Sign in OK
		{
			$user = user::model()->find(array("condition"=>"username = '" . $username ."'"));
			
			
			if ($user->backend == 1 && $user->enable == 1)
			{
				//Luu thong tin trong sessiion
				$session = new CDbHttpSession();
				
				//ID of the CDbConnection application component: config/main.php
				$session->connectionID = "db"; 
				$session_time = $this->data["website"]["session_time"];
				$session->setTimeout($session_time * 60);
				//$session->setSessionID(MyHelper::rand_string(50));
				
				$session->open();
				$datetime = getdate();
				$session["username"] = $username;
				
				$username = $session["username"];
				$session["user_logged_in"] = 1;
				
				//$session["language_code"] = Yii::app()->getRequest()->getPost("language_code");;
				
				$session->close();
				
				$user = user::model()->find(array("condition"=>"username = '".$username."'"));
				
				$user->last_visit = MyHelper::get_date()." ".MyHelper::get_time();
				$user->last_ipaddress = Yii::app()->getRequest()->getUserHostAddress();
				
				$session_current = session::model()->find(array(
					"condition"=>"id = '" . $session->getSessionID()."'"
				));
				
				$session_current->user_id = $user->id;
				$session_current->time_online = MyHelper::get_date()." ".MyHelper::get_time();
				$session_current->ip_online = Yii::app()->getRequest()->getUserHostAddress();
				$session_current->save();
				
				$user->save();
				
				$this->redirect(array("/".Yii::app()->defaultController));
				//$this->redirect(array("/system/user"));	
			}
			else
			{
				$this->data["message"] = "You are not allowed login.";
				$this->renderPartial('login_screen', $this->data);
			}
		}else{
			$this->data["message"] = "Username and password do not match.";
			$this->renderPartial('login_screen', $this->data);
		}	
		
	}
	
	function actionSign_out()
	{
		$session = new CDbHttpSession();
		//ID of the CDbConnection application component: config/main.php
		$session->connectionID = "db";
		$session->open();
		$session->destroy();		
		$this->redirect(array($this->data['controller_id'].'/index'));
	}
	
	
}